﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using KDS.Mes.Mes.PlanModes.Dtos;
using KDS.Mes.Authorization;

namespace KDS.Mes.Mes.PlanModes
{
    /// <summary>
    /// 计划模式信息服务实现
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Plan_PlanModes)]
    public class PlanModeAppService : MesAppServiceBase, IPlanModeAppService
    {
        private readonly IRepository<PlanMode, int> _planModeRepository;


        private readonly PlanModeManage _planModeManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public PlanModeAppService(IRepository<PlanMode, int> planModeRepository,
PlanModeManage planModeManage

  )
        {
            _planModeRepository = planModeRepository;
            _planModeManage = planModeManage;

        }

        #region 计划模式信息管理

        public async Task<ListResultDto<PlanModeListDto>> GetPlanModes(GetPlanModeInput input)
        {
            var planModes = await _planModeRepository.GetAll()
                .ToListAsync();

            return new ListResultDto<PlanModeListDto>(planModes.MapTo<List<PlanModeListDto>>());
        }

        public PagedResultDto<PlanModeListDto> GetPagedPlanModes(GetPlanModeInput input)
        {
            //初步过滤
            var query = _planModeRepository.GetAll();
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CreationTime);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();
            return new PagedResultDto<PlanModeListDto>(tasksCount, taskList.MapTo<List<PlanModeListDto>>());
        }

        /// <summary>
        /// 根据查询条件获取计划模式信息分页列表
        /// </summary>
        public async Task<PagedResultDto<PlanModeListDto>> GetPagedPlanModesAsync(GetPlanModeInput input)
        {

            var query = _planModeRepository.GetAll();
            //TODO:根据传入的参数添加过滤条件

            var planModeCount = await query.CountAsync();

            var planModes = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var planModeListDtos = planModes.MapTo<List<PlanModeListDto>>();
            return new PagedResultDto<PlanModeListDto>(
            planModeCount,
            planModeListDtos
            );
        }

        /// <summary>
        /// 通过Id获取计划模式信息信息进行编辑或修改 
        /// </summary>
        public async Task<GetPlanModeForEditOutput> GetPlanModeForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetPlanModeForEditOutput();

            PlanModeEditDto planModeEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _planModeRepository.GetAsync(input.Id.Value);
                planModeEditDto = entity.MapTo<PlanModeEditDto>();
            }
            else
            {
                planModeEditDto = new PlanModeEditDto();
            }

            output.PlanMode = planModeEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取计划模式信息ListDto信息
        /// </summary>
        public async Task<PlanModeListDto> GetPlanModeByIdAsync(EntityDto<int> input)
        {
            var entity = await _planModeRepository.GetAsync(input.Id);

            return entity.MapTo<PlanModeListDto>();
        }







        /// <summary>
        /// 新增或更改计划模式信息
        /// </summary>
        public async Task CreateOrUpdatePlanModeAsync(CreateOrUpdatePlanModeInput input)
        {
            if (input.PlanModeEditDto.Id.HasValue)
            {
                await UpdatePlanModeAsync(input.PlanModeEditDto);
            }
            else
            {
                await CreatePlanModeAsync(input.PlanModeEditDto);
            }
        }

        /// <summary>
        /// 新增计划模式信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Plan_PlanModes_Create)]
        public virtual async Task<PlanModeEditDto> CreatePlanModeAsync(PlanModeEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<PlanMode>();

            entity = await _planModeRepository.InsertAsync(entity);
            return entity.MapTo<PlanModeEditDto>();
        }

        /// <summary>
        /// 编辑计划模式信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Plan_PlanModes_Edit)]
        public virtual async Task UpdatePlanModeAsync(PlanModeEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _planModeRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _planModeRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除计划模式信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Plan_PlanModes_Delete)]
        public async Task DeletePlanModeAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _planModeRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除计划模式信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Plan_PlanModes_Delete)]
        public async Task BatchDeletePlanModeAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _planModeRepository.DeleteAsync(s => input.Contains(s.Id));

        }

        #endregion












    }
}
